
######################################################################
#
#  FLISM RDFa rendering...
#
######################################################################

# Here we have an override for the rende_single_value sub routine.
# This does what render_single_value does except it snaffles in 
# some RDFa based on the contents of lang/en/phrases/flism_rdfa_mappings.xml

my $flism_span_count = 0;
 
$c->{field_defaults}->{render_single_value} = sub
{
	my( $session, $field, $value) = @_;
	my $fieldname = $field->get_property("name");
	my (@properties, @typeofs, @abouts);
	my $rdfa_data = { };

	$session->get_repository->call("find_rdfa",$session,$fieldname,$rdfa_data,"xml:lang");
	$session->get_repository->call("find_rdfa",$session,$fieldname,$rdfa_data,"property");
	$session->get_repository->call("find_rdfa",$session,$fieldname,$rdfa_data,"typeof");
	$session->get_repository->call("find_rdfa",$session,$fieldname,$rdfa_data,"about");

	if(scalar keys %$rdfa_data){
		my @vals = ($value); 
		$flism_span_count++;
		my $rdfa_spans_cont = $session->make_doc_fragment;
		my $i=0;
		for my $val(@vals){
			my $fieldname_label = $fieldname;
			$fieldname_label =~ s/_/ /g;
			$fieldname_label = ucfirst $fieldname_label;
			my $rdfa_spans = $session->make_element("span", class=>"rdfas", rel=>$fieldname_label, id=>$fieldname."_".$flism_span_count);
			my $inner_span = $rdfa_spans;
			my $content = $val;
			if($field->get_property("type") eq "name"){
				$content = EPrints::Utils::make_name_string( $val, 1 );
			}
	
			while(my($class,$rdfa_values) = each(%$rdfa_data)) {
				for my $rdfa_value (@$rdfa_values){
					my $rdfa_span = $session->make_element("span", $class=>$rdfa_value, content=>$content);
					$inner_span->appendChild($rdfa_span);
					$inner_span = $rdfa_span;
				}
			}
	
			if($field->get_property("type") eq "name"){
				$inner_span->appendChild($session->render_name($val) );
			}else{
				if($val){
				   $val = HTML::Entities::decode_entities($val);
	       			}
				$inner_span->appendChild( $session->make_text($val) );
			}
			$rdfa_spans_cont->appendChild($rdfa_spans);
			$i++;
		}	
		return $rdfa_spans_cont;
	}else{
		if($value){
			if($field->get_property("type") eq "name"){
				$value = EPrints::Utils::make_name_string( $value, 1 );
			}
			$value = HTML::Entities::decode_entities($value);
       		}
		return $session->make_text( $value );
	}
};

$c->{find_rdfa} = sub 
{
	my ($session, $fieldname, $rdfa_data, $class) = @_;
	if( $session->get_lang->has_phrase( "rdfa_".$class."_".$fieldname) ) { 
		my @data = split /,/, $session->phrase( "rdfa_".$class."_".$fieldname ); 
		$rdfa_data->{$class} = \@data;
	}
	return $rdfa_data;
};
